
function renderTemplate(obj,result) {
	//console.log(result.sourceStr.dataset.source)
	//console.log(result.sourceStr.dataset.dimensions)
	var tpl = $('script',obj).html()
	console.log(tpl)
	var html = baidu.template(tpl,
		{
		    rows:result.sourceStr.dataset.source,
		    cols:result.sourceStr.dataset.dimensions
		}
	);  
	$(obj).html(html)
}

function renderChart(obj,result) {
	//console.log(obj)
	if (result.themeName!=null && result.themeName!="") {
		console.log(JSON.parse(result.themeSource))
		echarts.registerTheme(result.themeName, JSON.parse(result.themeSource))
        var chart = echarts.init(obj,result.themeName);
        chart.setOption(result.sourceStr)
	} else {
		var chart = echarts.init(obj);
    	chart.setOption(result.sourceStr)
	}
	
}


function renderText(obj,result) {
	var content = result.sourceStr.content
	for (var i=0;i<result.sourceStr.dataset.dimensions.length;i++) {
		var data = result.sourceStr.dataset.source[0]
		content = content.replace('{'+result.sourceStr.dataset.dimensions[i]+'}',data[result.sourceStr.dataset.dimensions[i]])
	}

	obj.html(content)

	
}

function renderTable(obj,result) {
	//widgetObject[wid]
	//console.log(obj.css("width"))

	//console.log(result.sourceStr)

	var thead = '<thead><tr style="background-color:#d4e3e5;">'
	for (var i=0;i<result.sourceStr.dataset.dimensions.length;i++) {
		thead +='<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;">'+result.sourceStr.dataset.dimensions[i]+'</th>'
	}
	thead +='</tr></thead>'
	
	var tbody = '<tbody>'
	for (var i=0;i<result.sourceStr.dataset.source.length;i++) {
		tbody += '<tr style="background-color:">'
		for (var j=0;j<result.sourceStr.dataset.dimensions.length;j++) {
			var fieldName = result.sourceStr.dataset.dimensions[j]
			tbody +='<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;">'+result.sourceStr.dataset.source[i][fieldName]+'</td>'
		}
		tbody +='</tr>'
	}
	tbody +='</tbody>'

	/*var temp = '<table class="table table-striped table-bordered" '+
	'style=""width:100%;font:90%/1.45em "Helvetica Neue", HelveticaNeue, Helvetica, Arial, sans-serif !important">'+thead+tbody+'</table>'
	*/
	var temp = '<table style="font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;'+
	'border-color: #a9c6c9;border-collapse: collapse;" >'+thead+tbody+'</table>'
	obj.html(temp)
	/*obj.children().DataTable(
		{
    		searching:false,
    		paging: false,
    		autoWidth:false,
    		'bAutoWidth': false,
    		info: false,
    		ordering:false
		}
	);*/
	

}

function getWidget(widgets) {
	//console.log(widgets)
	    
	if (widgets.length!=0) {
		var wid = $(widgets[0]).attr('wid')
		var wp = $(widgets[0]).attr('wp')
	
		$.ajax({
			url:'http://'+widgetServer+'/widget/render',
	        dataType:'jsonp',
	        //jsonp: "callback",
	        jsonpCallback: "success_callback",
	        data:{
	            wid:wid,
	            wp:wp
	        },
	        success:function(result){
	        	//console.log(result)
	        	loadedWidget++
				$('#progressIndiator').html(parseInt(loadedWidget*100/totalWidget)+"%")

	        	if (result.success) {
		        	if ($(widgetObject[wid]).attr('useTemplate')!=null||$(widgetObject[wid]).attr('useTemplate')=="true") {
		        		renderTemplate($(widgetObject[wid]),result)
					} else {
						if (result.widgetType=="表格") {
							renderTable($(widgetObject[wid]),result)
			            }
			            if (result.widgetType=="文本") {
							renderText($(widgetObject[wid]),result)
			            }
			            if (result.widgetType==null||result.widgetType=="") {
							renderChart(widgetObject[wid],result)
			            }
					}
		            
	        	}
	        	widgets.splice(0,1)
		        getWidget(widgets)
	        } ,
	        error:function(err){
	        	console.log(err)
	        	loadedWidget++
				$('#progressIndiator').html((loadedWidget*100/totalWidget)+"%")
	        	widgets.splice(0,1)
	        	getWidget(widgets)
	        }
	    });

    } else {
    	$('body').show()
    	$('#loadingIndiator').remove()
    }

}

var widgetObject = {}

var totalWidget = 0

var loadedWidget = 0
    
$(document).ready(function(){


	$('body').before("<span id='loadingIndiator'>正在读取数据....<span id='progressIndiator'></span></span>")
	$('#loadingIndiator').show()
	var widgetsArray = []
	var widgets = $("[wid]")
    totalWidget = widgets.length
    for(var i=0;i<widgets.length;i++) {
	    widgetsArray.push($(widgets[i]))
	    //console.log($(widgets[i]).attr('wid'))
	    widgetObject[$(widgets[i]).attr('wid')] = widgets[i]
	}

	//console.log(widgetObject)
	

    getWidget(widgetsArray)
    //var widgets = $("[attribute='token']")
    
});